package com.xuxin.baby.model;

import com.xuxin.baby.bean.BaseBean;
import com.xuxin.baby.bean.UserDataBean;
import com.xuxin.baby.contract.UserContract;
import com.xuxin.baby.network.Retrofit;
import com.xuxin.baby.network.RetrofitApi;
import com.xuxin.baby.network.myObserver;
import com.xuxin.baby.presenter.UserPresenter;

import org.json.JSONException;
import org.json.JSONObject;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.observers.DisposableObserver;
import io.reactivex.rxjava3.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by xu_xin
 * Created on 2020/10/23 0023
 * 个人信息
 */
public class UserModel implements UserContract.Model {
    @Override
    public void getUserData(UserPresenter presenter, String tenantId, String token, String memberId) {
        Retrofit.getInstance().createReq(RetrofitApi.class)
                .getUserData(tenantId, token,"1", memberId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new myObserver<UserDataBean>() {
                    @Override
                    public void onNextX(@NonNull UserDataBean baseBean) {
                        if (baseBean.isSuccess()) {
                            presenter.onSuccess(baseBean);
                        } else {
                            presenter.onError(baseBean);
                        }
                    }
                    @Override
                    public void onError(BaseBean baseBean) {
                        presenter.onError(baseBean);
                    }
                    @Override
                    public void onError(@NonNull Throwable e) {
                        presenter.onError(null);
                    }

                    @Override
                    public void onComplete() {
                        presenter.onComplete();
                    }
                });
    }

    @Override
    public void uploadAvatar(UserPresenter presenter, String tenantId, String token, String memberId, String avatar) {
        Retrofit.getInstance().createReq(RetrofitApi.class)
                .uploadAvatar(tenantId,
                        token,"1",
                        memberId,
                        avatar)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new myObserver<BaseBean>() {
                    @Override
                    public void onNextX(@NonNull BaseBean baseBean) {
                        if (baseBean.isSuccess()) {
                            presenter.onImageSuccess(baseBean);
                        } else {
                            presenter.onError(baseBean);
                        }
                    }
                    @Override
                    public void onError(BaseBean baseBean) {
                        presenter.onError(baseBean);
                    }
                    @Override
                    public void onError(@NonNull Throwable e) {
                        presenter.onError(null);
                    }

                    @Override
                    public void onComplete() {
                        presenter.onComplete();
                    }
                });
    }

    @Override
    public void updateUserData(UserPresenter presenter, String tenantId, String token, String memberId, String mobile,String avatar,
                               String nick, String birthday, String nation, String role, String birthAddress, String address) {
        try {
            JSONObject object = new JSONObject();
            object.put("id", memberId);
            object.put("mobile", mobile);
            object.put("avatar", avatar);
            object.put("nickname", nick);
            object.put("birthday", birthday);
            object.put("ethnicity", nation);
            object.put("role", role);
            object.put("birthAddress", birthAddress);
            object.put("address", address);
            RequestBody requestBody = RequestBody.create(object.toString(), MediaType.parse("application/json"));
            Retrofit.getInstance().createReq(RetrofitApi.class)
                    .updateUserData(tenantId,
                            token,"1",
                            requestBody)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new myObserver<BaseBean>() {
                        @Override
                        public void onNextX(@NonNull BaseBean baseBean) {
                            if (baseBean.isSuccess()) {
                                presenter.onSuccess(baseBean);
                            } else {
                                presenter.onError(baseBean);
                            }
                        }
                        @Override
                        public void onError(BaseBean baseBean) {
                            presenter.onError(baseBean);
                        }
                        @Override
                        public void onError(@NonNull Throwable e) {
                            presenter.onError(null);
                        }

                        @Override
                        public void onComplete() {
                            presenter.onComplete();
                        }
                    });
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
}